﻿app.directive("openxtcheckacct", function () {
    return {
        link: function (scope, element, attrs) {
            var errmsg = element.attr("openxtcheckacct");
            if (errmsg == undefined || errmsg == "") {
                errmsg = "账号格式错误";
            }
            element.on("blur", function () {
                var o = element.val();
                var r = (o != undefined) && /^([\u4e00-\u9fa5_a-zA-Z0-9]){2,16}$/.test(o);
                if (r == false) {
                    scope.errorMesssage = errmsg;
                    element.attr("openxtcheckresult", 0);
                    element.css("border-color", "red");
                } else {
                    element.attr("openxtcheckresult", 1);
                    scope.errorMesssage = "";
                    element.css("border-color", "#DCE4EC");
                }
                scope.$apply(attrs.openxtcheckacct);
            });
        }
    }
});
app.directive("openxtcheckpwd", function () {
    return {
        link: function (scope, element, attrs) {
            var errmsg = element.attr("openxtcheckpwd");
            if (errmsg == undefined || errmsg == "") {
                errmsg = "密码格式错误";
            }
            element.on("blur", function () {
                var o = element.val();
                var r = (o != undefined) && /^([A-Za-z0-9]){2,16}$/.test(o);
                if (r == false) {
                    scope.errorMesssage = errmsg;
                    element.attr("openxtcheckresult", 0);
                    element.css("border-color", "red");
                } else {
                    scope.errorMesssage = "";
                    element.attr("openxtcheckresult", 1);
                    element.css("border-color", "#DCE4EC");
                }
                scope.$apply(attrs.openxtcheckpwd);
            });
        }
    }
});
app.directive("openxtcheckpwd2", function () {
    return {
        link: function (scope, element, attrs) {
            element.on("blur", function () {
                var pwdElement = element.attr("openxtcheckpwd2");
                if (pwdElement == undefined || pwd == "") {
                    return;
                }
                var errmsg = $("#" + pwdElement).attr("openxtcheckpwd");
                if (errmsg == undefined || errmsg == "") {
                    errmsg = "密码格式错误";
                }
                var o = element.val();
                var r = (o != undefined) && /^([A-Za-z0-9]){2,16}$/.test(o);
                if (r == false) {
                    scope.errorMesssage = errmsg;
                    element.attr("openxtcheckresult", 0);
                    element.css("border-color", "red");
                } else {
                    var pwd = $("#" + pwdElement).val();
                    if (pwd != o) {
                        scope.errorMesssage = "密码输入不一致";
                        element.attr("openxtcheckresult", 0);
                        element.css("border-color", "red");
                    } else {
                        scope.errorMesssage = "";
                        element.attr("openxtcheckresult", 1);
                        element.css("border-color", "#DCE4EC");
                    }
                }
                scope.$apply(attrs.openxtcheckpwd2);
            });
        }
    }
});
app.directive("openxtrequired", function () {
    return {
        link: function (scope, element, attrs) {
            var errmsg = element.attr("openxtrequired");
            if (errmsg == undefined || errmsg == "") {
                errmsg = "内容不能为空";
            }
            element.on("blur", function () {
                var o = element.val();
                o = o.replace(/^\s*/, "").replace(/\s*$/, "");
                var r = (o != undefined) && o.length > 0;
                if (r == false) {
                    scope.errorMesssage = errmsg;
                    element.attr("openxtcheckresult", 0);
                    element.css("border-color", "red");
                } else {
                    scope.errorMesssage = "";
                    element.attr("openxtcheckresult", 1);
                    element.css("border-color", "#DCE4EC");
                }
                scope.$apply(attrs.openxtcheckpwd);
            });
        }
    }
});
app.directive("openxtonscrolldown", function ($timeout) {
    return {
        link: function (scope, element, attrs) {
            var fn = attrs["openxtonscrolldown"];
            scope.downbeforeScrollTop = $(document).scrollTop();
            $(window).on("scroll", function () {
                var clienth = document.documentElement.clientHeight;  //屏幕高度
                var scrollt = document.documentElement.scrollTop + document.body.scrollTop; //滚动条滚动高度
                //var allHeight = Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight); //整个页面高度   
                var allheight2 = Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
                //alert("屏幕高度=" + clienth + "   滚动条滚动高度" + scrollt + "    整个页面高度" + allHeight);
                //alert(allheight2);
                //var v = $(document).scrollTop() - scope.downbeforeScrollTop;
                var v = (clienth + scrollt - allheight2);
                if (v >= 0) {
                    $timeout(function () {
                        scope.$applyAsync(function () {
                            eval(fn);
                        });
                    });
                }
                scope.downbeforeScrollTop = v;
            });
        }
    }
});
app.directive("openxtonscrollup", function ($timeout) {
    return {
        link: function (scope, element, attrs) {
            var fn = attrs["openxtonscrollup"];
            scope.upbeforeScrollTop = $(document).scrollTop();
            $(window).on("scroll", function () {
                var v = $(document).scrollTop() - scope.upbeforeScrollTop;
                if (v < 0) {
                    $timeout(function () {
                        scope.$apply(function () {
                            eval(fn);
                        });
                    });
                }
                scope.upbeforeScrollTop = v;
            });
        }
    }
});
app.directive("openxtcheckfloat", function () {
    return {
        link: function (scope, element, attrs) {
            var errmsg = element.attr("openxtcheckfloat");
            if (errmsg == undefined || errmsg == "") {
                errmsg = "数值格式错误";
            }
            element.on("blur", function () {
                var o = element.val();
                var r = ((o != undefined) && /(^(-)*\d+\.\d*$)|(^(-)*\d+$)/.test(o));
                if (r == false) {
                    scope.errorMesssage = errmsg;
                    element.attr("openxtcheckresult", 0);
                    element.css("border-color", "red");
                } else {
                    element.attr("openxtcheckresult", 1);
                    scope.errorMesssage = "";
                    element.css("border-color", "#DCE4EC");
                }
                scope.$apply(attrs.openxtcheckfloat);
            });
        }
    }
});


app.directive("zjwcheckacct", function () {
    return {
        link: function (scope, element, attrs) {
            var errmsg = element.attr("zjwcheckacct");
            if (errmsg == undefined || errmsg == "") {
                errmsg = "手机号格式错误";
            }
            element.on("blur", function () {
                var o = element.val();
                var r = (o != undefined) && /^(13[0-9]|15[0|1|3|6|7|8|9]|18[8|9])\d{8}$/.test(o);
                if (r == false) {
                    scope.errorMesssage = errmsg;
                    element.attr("openxtcheckresult", 0);
                    element.css("border-color", "red");
                } else {
                    element.attr("openxtcheckresult", 1);
                    scope.errorMesssage = "";
                    element.css("border-color", "#DCE4EC");
                }
                scope.$apply(attrs.openxtcheckacct);
            });
        }
    }
});
app.directive('repeatDone', function ($timeout) {
    return {
        link: function (scope, element, attrs) {
            if (scope.$last) {
                $timeout(function () {// 这个判断意味着最后一个 OK
                    scope.$eval(attrs.repeatDone)
                })// 执行绑定的表达式
            }
        }
    }
})
//app.directive("swiperDirective", function ($rootScope) {
//    return {
//        link: function (scope, element, attrs, ctrl) {
//            mySwiper = new Swiper('.input-style2 .swiper-container', {
//                pagination: '.input-style2 .swiper-pagination',
//                loop: false
//            });
//        }
//    }
//});
//app.directive('onFinishRenderFilters', function ($timeout) {
//    return {
//        restrict: 'A',
//        link: function (scope, element, attr) {
//            if (scope.$last === true) {
//                $timeout(function () {
//                    scope.$emit('ngRepeatFinished');
//                });
//            }
//        }
//    };
//});
